package com.Interface.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.coobird.thumbnailator.Thumbnails;

import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.Interface.util.StaticValue;

@Controller
@RequestMapping("/picture") 
public class PictureController {
	public  final static String PROJECT_NAME="photo";
	/**
	 * 编辑器文件上传
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	 @ResponseBody
	 @RequestMapping(value = "/fileUpload", produces = MediaType.APPLICATION_JSON_VALUE)
	public Map FileUpload(HttpServletRequest request, @RequestPart("file") MultipartFile file) throws Exception {
		 String width=request.getParameter("width");
		 String height=request.getParameter("height");
		 int  s=request.getServletContext().getRealPath("/").lastIndexOf("webapps");
	     String path=request.getServletContext().getRealPath("/").substring(0, s)+"/webapps/"+StaticValue.PROJECT_NAME;
	     Map<String ,Object> map = new HashMap<String ,Object>();
	     if(file.getSize()>0){
				Calendar date = Calendar.getInstance();//按日期创建文件夹
				String upload_file=File.separator + date.get(Calendar.YEAR)
		                + File.separator + (date.get(Calendar.MONTH)+1) + File.separator
		                + date.get(Calendar.DAY_OF_MONTH);
		        File file_upload = new File(path+upload_file);
		        if(!file_upload.exists()){//目录不存在则直接创建
		        	file_upload.mkdirs();
		        }
				String fileName = file.getOriginalFilename();  
		        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); 
				String datePath =  File.separator +new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new java.util.Date())+"."+suffix;
				File imageFile = new File(file_upload+datePath);
				
				try {
					file.transferTo(imageFile);
					//不按比例，就按指定的大小进行缩放
					Thumbnails.of(imageFile).size(Integer.parseInt(width),Integer.parseInt(height)).keepAspectRatio(false).toFile(imageFile);
					map.put("code", 200);
					map.put("msg", "操作成功");
					Map<String ,Object> m=new HashMap<String ,Object>();
					String IP_PORT="";
					try {
						IP_PORT=PropertiesLoaderUtils.loadAllProperties("config.properties").getProperty("ip_port");
					} catch (IOException e) {
						e.printStackTrace();
					}
					m.put("url", IP_PORT+StaticValue.PROJECT_NAME+upload_file+datePath);
					map.put("data", m);
				} catch (IllegalStateException e) {
					e.printStackTrace();
					map.put("code", 400);
					map.put("msg", "上传失败");
				} catch (IOException e) {
					map.put("code", 400);
					map.put("msg", "上传失败");
				}
			}else{
				map.put("code", 400);
				map.put("msg", "未上传文件");
			}
		return map;
	 
	}
 /**
	 * 编辑器文件上传
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	 @ResponseBody
	 @RequestMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE)
	public Map upload(HttpServletRequest request, @RequestPart("file") MultipartFile file) throws Exception {
		 int  s=request.getServletContext().getRealPath("/").lastIndexOf("webapps");
	     String path=request.getServletContext().getRealPath("/").substring(0, s)+"/webapps/"+StaticValue.PROJECT_NAME;
	     Map<String ,Object> map = new HashMap<String ,Object>();
	     if(file.getSize()>0){//有文件长传
				Calendar date = Calendar.getInstance();//按日期创建文件夹
				String upload_file=File.separator + date.get(Calendar.YEAR)
		                + File.separator + (date.get(Calendar.MONTH)+1) + File.separator
		                + date.get(Calendar.DAY_OF_MONTH);
		        File file_upload = new File(path+upload_file);
		        if(!file_upload.exists()){//目录不存在则直接创建
		        	file_upload.mkdirs();
		        }
				String fileName = file.getOriginalFilename();  
		        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); 
				String datePath =  File.separator +new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new java.util.Date())+"."+suffix;
				File imageFile = new File(file_upload+datePath);
				
				try {
					file.transferTo(imageFile);
					List<String > list =new ArrayList<String>();
					String IP_PORT="";
						try {
							IP_PORT=PropertiesLoaderUtils.loadAllProperties("config.properties").getProperty("ip_port");
						} catch (IOException e) {
							e.printStackTrace();
						}
					
					list.add(IP_PORT+StaticValue.PROJECT_NAME+upload_file+datePath);
					map.put("errno", 0);						
					map.put("data", list);
				} catch (IllegalStateException e) {
					e.printStackTrace();
					map.put("code", 400);
					map.put("msg", "上传失败");
				} catch (IOException e) {
					map.put("code", 400);
					map.put("msg", "上传失败");
				}
			}else{
				map.put("code", 400);
				map.put("msg", "未上传文件");
			}
		return map;
	 
	}
}
